package com.guigu.leizihao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.mapper.SCellMapper;
import com.guigu.common.mapper.SPayDetailsMapper;
import com.guigu.common.mapper.SPayMapper;
import com.guigu.common.pojo.SPay;
import com.guigu.common.pojo.SPayDetails;
import com.guigu.common.pojo.SysUsers;
import com.guigu.common.util.GetUUidUtil;
import com.guigu.common.vo.SearchVo;
import com.guigu.leizihao.service.SpayService;
import com.guigu.leizihao.vo.SGatherVo;
import com.guigu.leizihao.vo.SpayDetailsView;
import com.guigu.leizihao.vo.SpayView;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SpayServiceImpl implements SpayService {

    @Resource
    private SPayMapper sPayMapper;

    @Resource
    private SPayDetailsMapper sPayDetailsMapper;

    @Resource
    private GetUUidUtil getUUidUtil;

    @Resource
    private SCellMapper sCellMapper;

    /**
     * 添加出库单 和出库明细
     * @param sGatherVo
     */
    @Override
    public void addSpayApplyFor(SGatherVo sGatherVo) {
        String s = getUUidUtil.returnId("402");
        SPay spay = sGatherVo.getSpay();
        spay.setPayId(s);
        spay.setCheckTag("s001-0");
        this.sPayMapper.addSpay(spay);
        Integer id = spay.getId();
        List<SPayDetails> list = sGatherVo.getList();
        addSpayDetail(list,id);
    }

    /**
     * 查询出库单待审核的
     * @param searchVo
     * @return
     */
    @Override
    public SearchVo queryspayToAudit(SearchVo searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(),searchVo.getPageSize());
        PageInfo p=new PageInfo(this.sPayMapper.querySpayToAudit(searchVo));
        searchVo.setTotal(p.getTotal());
        searchVo.setRows(p.getList());
        return searchVo;
    }

    @Override
    public List<SpayView> findSpayDetailsById(Integer id) {

        return  this.sPayDetailsMapper.findSpayDetailsById(id);
    }

    @Override
    public int changeSpay(SPay sPay) {
        this.sPayDetailsMapper.sPayDetailsPayTag(sPay.getId());
        return this.sPayMapper.changeSpayAA(sPay);
    }

    @Override
    public SearchVo querySpayAll(SearchVo searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(),searchVo.getPageSize());
        PageInfo p=new PageInfo(this.sPayMapper.querySpayAlla(searchVo));
        searchVo.setTotal(p.getTotal());
        searchVo.setRows(p.getList());
        return searchVo;
    }

    @Override
    public SearchVo findFormulateSpay(SearchVo searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(),searchVo.getPageSize());
        PageInfo p=new PageInfo(this.sPayMapper.findFormulateSpaya(searchVo));
        searchVo.setTotal(p.getTotal());
        searchVo.setRows(p.getList());
        return searchVo;
    }

    @Override
    public int UpdateSpay_details(Integer id) {


        this.sPayDetailsMapper.UpdateSpay_details(id);
        if(this.sPayDetailsMapper.querySpay_detailsCount(id)==0){
            SPay spay=new SPay();
            spay.setId(id);
            spay.setAttemper(((SysUsers)SecurityUtils.getSubject().getSession().getAttribute("user")).getLoginId());
            this.sPayMapper.updateStore_tagByid(spay);
        }
        return 0;
    }

    @Override
    public SearchVo querySpayOut(SearchVo searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(),searchVo.getPageSize());
        PageInfo p=new PageInfo(this.sPayMapper.querySpayOut(searchVo));
        searchVo.setTotal(p.getTotal());
        searchVo.setRows(p.getList());
        return searchVo;

    }

    @Override
    public void updateSpay_detailsQuantityInStorage(SGatherVo sGatherVo) {
        this.sPayDetailsMapper.updateSpay_detailsQuantityInStorage(sGatherVo.getList());
    }

    @Override
    public SearchVo queryOutboundCheck(SearchVo searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(),searchVo.getPageSize());
        PageInfo p=new PageInfo(this.sPayMapper.queryOutboundCheck(searchVo));
        searchVo.setTotal(p.getTotal());
        searchVo.setRows(p.getList());
        return searchVo;
    }

    @Override
    public List<SpayView> findSpayDetailsByIdandQuantityIinStorage(Integer id) {

      return   this.sPayDetailsMapper.findSpayDetailsByIdandQuantityIinStorage(id);
    }

    /**
     * 通过审核更改出库数量
     * @param sGatherVo
     */
    @Override
    public void OutboundCheckPass(SGatherVo<SPayDetails> sGatherVo) {
        this.sPayDetailsMapper.updateSpayDetailsPaidAmount(sGatherVo.getList());
        this.sPayMapper.updateSpayPaidAcountSum(sGatherVo.getSpay().getPayId());
        this.sCellMapper.updateCellOutAcount(sGatherVo.getList());
        this.sPayDetailsMapper.updateSpayDetailsPayTagById(sGatherVo.getList());
        SPay spay = sGatherVo.getSpay();
        SysUsers user =(SysUsers) SecurityUtils.getSubject().getSession().getAttribute("user");
        spay.setAttemper(user.getLoginId());
        this.sPayMapper.updateSpayPatTagByPayId(spay);
        System.out.println(sGatherVo);
    }

    @Override
    public Integer querySpayTotalcost(Integer id) {
      return  this.sPayDetailsMapper.querySpayTotalcost(id);
    }

    @Override
    public List<SpayDetailsView> queryspayDetailsByproductId(String productid) {
        return this.sPayDetailsMapper.queryspayDetailsByproductId(productid);
    }

    @Override
    public void changeSpayNo(Integer id) {
        SysUsers user =(SysUsers) SecurityUtils.getSubject().getSession().getAttribute("user");
        SPay s=new SPay();
        s.setId(id);
        s.setChecker(user.getLoginId());
        System.out.println(s);
        this.sPayMapper.changeSpayNo(s);
    }

    /**
     * 添加出库明细
     * @param list
     * @param id
     */
    public void addSpayDetail(List<SPayDetails> list, Integer id){
        System.out.println(list);
        for (SPayDetails d:list){
            d.setParentId(id);
//            d.setPayTag("k002-1");
        }
        this.sPayDetailsMapper.addSpayDetail(list);
    }


}
